package com.zdsoft.blp.zongbuju.shouye.linliquan.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.myFragment.CustomFragment;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.xlistview.XListView;
import com.zdsoft.blp.zongbuju.shouye.linliquan.activity.LLZuLinActivity;
import com.zdsoft.blp.zongbuju.shouye.linliquan.activity.XQQZZPActivity;
import com.zdsoft.blp.zongbuju.shouye.linliquan.activity.XQZSActivity;
import com.zdsoft.blp.zongbuju.shouye.linliquan.adapter.SheQuLunTanAdapter;
import com.zdsoft.blp.zongbuju.shouye.linliquan.entity.LinLi;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.tencent.open.utils.Global.getSharedPreferences;

/**
 * Created by Administrator on 2016/9/2.
 */
public class ZLfragmentRight extends CustomFragment implements NetCallback,XListView.IXListViewListener {
    private XListView xlv;
    private List<LinLi> list;
    private List<LinLi> mlist;
    private int pageNo=1;
    private SheQuLunTanAdapter adapter;
    private String  touxiang,userName,fbtime,title,content,fbName,dianhua,contentImage
            ,pinglun,id;
    private int source;
    @Override
    protected void init() {
        initLayout(R.layout.linli_fragment_es);
        xlv=find(R.id.xlv_ll_es);
        xlv.setXListViewListener(this);
        xlv.setPullRefreshEnable(true);//设置可以刷新
        xlv.setPullLoadEnable(true);//设置可以加载更多
        LLZuLinActivity ac= (LLZuLinActivity) getActivity();
        source=ac.source;
        getNet();

        xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(),XQZSActivity.class);
                intent.putExtra("id",list.get(position-1).getId());
                intent.putExtra("title","出售详情");
                startActivity(intent);

            }
        });
    }
    //实例化Net 请求网络，分页获取房屋出租信息列表
    public void getNet(){
        Net net = new Net();
        net.url = API.INFOEXCHANGE_URL;
        net.nameSpace = API.INFOEXCHANGE_NAMESPACE;
        Map<String, Object> map = new HashMap<>();
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String userId = sp.getString("userId","");
        String communityCode = sp.getString("communityCode","00001");
        if (source==2){
            net.methodName = API.MYHOUSERENTALIST;
            map.put("pageNo",1);
            map.put("pageSize",20);
            map.put("communityCode",communityCode);
            map.put("infoType","cs");
            map.put("condition","");
            map.put("curTime","");
            map.put("userId",userId);
            net.code=2;
            net.parms = map;
        }else {
            net.methodName = API.HOUSERENTALLIST;
            map.put("pageNo",1);
            map.put("pageSize",20);
            map.put("communityCode",communityCode);
            map.put("infoType","cs");
            map.put("condition","");
            map.put("curTime","");
            net.code=1;
            net.parms = map;
        }

        ((LLZuLinActivity) getActivity()).getServer(net, this);

    }
    //实现NetCallback接口，并重写OnResponse方法，解析json数据,赋值给list集合
    @Override
    public void onResponse(boolean flag, int code, String res) {
        Log.e("房屋租凭",res);
        if (flag){
            if (pageNo==1){
                mlist=jiexi(code,res);
                adapter = new SheQuLunTanAdapter(mlist, getActivity());
                xlv.setAdapter(adapter);
                xlv.stopRefresh();
            }else {
                list=jiexi(code,res);
                for (LinLi linLis:list){
                    mlist.add(linLis);
                }
                adapter.setListData(mlist);
                xlv.stopLoadMore();
            }
        }
    }

    private List<LinLi> jiexi(int code, String res) {
        list = new ArrayList<>();
        LinLi fangwu=null;
        try {
            JSONObject objec = new JSONObject(res);
            JSONArray array = objec.getJSONArray("rows");
            for (int i = 0; i < array.length(); i++) {
                JSONObject object = array.getJSONObject(i);
                id= object.getString("id");
                touxiang= object.getString("headimgurl");
                userName= object.getString("userName");
                fbtime=object.getString("createTime");
                title= object.getString("title");
                content= object.getString("content");
                fbName=object.getString("contact");
                dianhua=object.getString("contactPhone");
                contentImage=object.getString("picPath");
                pinglun= object.getString("replyNum");
                int approvedResult=object.getInt("approvedResult");
                if (code==1){
                    fangwu = new LinLi(id,touxiang,userName,fbtime,title,content,fbName,dianhua,contentImage,pinglun );
                    fangwu.setCode(5);
                }
                if (code==2){
                    fangwu = new LinLi(id,touxiang,userName,fbtime,title,content,fbName,dianhua,contentImage,pinglun ,approvedResult);
                    fangwu.setCode(52);
                }

                list.add(fangwu);
            }
            xlv.setAdapter(new SheQuLunTanAdapter(list,getActivity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void onRefresh() {
        mlist=new ArrayList<>();
        pageNo=1;
        getNet();
    }

    @Override
    public void onLoadMore() {
        pageNo++;
        getNet();
    }
}
